package com.sfzd5.exam.helplibrary.exam;

import com.sfzd5.exam.helplibrary.exam.FontMetricsMM;
import com.sfzd5.exam.helplibrary.exam.PageA4;
import com.swetake.util.Qrcode;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;

/**
 * 字号处理办法
 * 高度即字体大小
 * 坐标计算 实际Y度假村，y = y + h - fm.getDescent();
 * Word字号高度计算：字号*25.4/72 ，单位 毫米，Word字号即点数
 * 毫米到点的计算：300*mm/25.4
 *
 * 目标，设计一个以毫米为单位的输出平台
 * 构建函数：
 * 1.输出方块，x,y,w,h
 * 2.输出文字，x,y,h
 * 3.输出图片，x,y,w,h
 * 4.输出方框，x,y,w,h
 * 5.画线, x1,y1,x2,y2
 */
public class Graphics2DMM {

    private Graphics2D g;

    public void dispose(){
        g.dispose();
    }

    public Graphics2DMM(Graphics2D g){
        this.g = g;
    }

    public void drawLine(float mx1, float my1, float mx2, float my2, float lineWidthm){
        float lineWidth = PageA4.mm2point(lineWidthm);
        //if(lineWidth==0) lineWidth=1;

        int x1 = (int)mm2point(mx1);
        int y1 = (int)mm2point(my1);
        int x2 = (int)mm2point(mx2);
        int y2 = (int)mm2point(my2);
        g.setStroke(new BasicStroke(lineWidth));
        g.drawLine(x1, y1, x2, y2);
    }

    public void drawRect(float mx, float my, float mw, float mh, float rectBoardWidthm){
        float rectBoardWidth = PageA4.mm2point(rectBoardWidthm);
        //if(rectBoardWidth==0) rectBoardWidth=1;
        float x = PageA4.mm2point(mx);
        float y = PageA4.mm2point(my);
        float w = PageA4.mm2point(mw);
        float h = PageA4.mm2point(mh);//即fontsize

        g.setStroke(new BasicStroke(rectBoardWidth));
        g.drawRect((int)x, (int)y, (int)w, (int)h);
    }

    public void drawImage(BufferedImage image, float mx, float my, float mw, float mh){
        float x = PageA4.mm2point(mx);
        float y = PageA4.mm2point(my);
        float w = PageA4.mm2point(mw);
        float h = PageA4.mm2point(mh);//即fontsize
        g.drawImage(image, (int)x, (int)y, (int)w, (int)h, null);
    }

    public void drawCodeEncode(String encodeddata, float mx, float my, float sizem, char errorCorrect, char mode, int version) {

        int x = (int)mm2point(mx);
        int y = (int)mm2point(my);
        float size = mm2point(sizem);

        Qrcode qrcode = new Qrcode();
        /**
         * 纠错等级分为
         * level L : 最大 7% 的错误能够被纠正；
         * level M : 最大 15% 的错误能够被纠正；
         * level Q : 最大 25% 的错误能够被纠正；
         * level H : 最大 30% 的错误能够被纠正；
         */
        qrcode.setQrcodeErrorCorrect(errorCorrect);    // 纠错级别（L 7%、M 15%、Q 25%、H 30%）和版本有关
        qrcode.setQrcodeEncodeMode(mode); ////注意版本信息 N代表数字 、A代表 a-z,A-Z、B代表 其他) B
        qrcode.setQrcodeVersion(version);        // 设置Qrcode包的版本 7
        byte[] d = new byte[0];    // 字符集
        try {
            d = encodeddata.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        boolean[][] b = qrcode.calQrcode(d);

        int pointCount = b.length; // 每行点数
        int pointWidth = (int)size / pointCount; //每点像素数，结合指定的宽度计算得出
        int border = (int)((size - pointWidth * pointCount) / 2); //边距，计算得出

        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b.length; j++) {
                if (b[j][i]) {
                    g.fillRect(j * pointWidth + x + border, i * pointWidth + y + border, pointWidth, pointWidth);
                }
            }
        }
    }
    public void drawCodeEncode(String encodeddata, float mx, float my, float sizem) {
        drawCodeEncode(encodeddata, mx, my, sizem, 'L', 'B', 0);
    }


    /**
     * 水平和垂直居中划线
     * @param str
     * @param mx
     * @param my
     * @param mh
     */
    public float drawString(String str, float mx, float my, float mh){
        float x = PageA4.mm2point(mx);
        float y = PageA4.mm2point(my);
        float h = PageA4.mm2point(mh);//即fontsize

        Font oldFont = g.getFont();
        Font font = new Font(oldFont.getName(), Font.PLAIN, Math.round(h));
        g.setFont(font);

        FontMetrics fm = g.getFontMetrics();
        y = y + h - fm.getDescent();
        g.drawString(str, x, y);

        Rectangle2D rc = fm.getStringBounds(str, g);
        return PageA4.point2mm((float) rc.getWidth());
    }

    public FontMetricsMM getFontMetricsMM(float m){
        float h = PageA4.mm2point(m);//即fontsize
        Font oldFont = g.getFont();
        Font font = new Font(oldFont.getName(), Font.PLAIN, Math.round(h));
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics();
        return new FontMetricsMM(fm, g);
    }

    /**
     * 水平和垂直居中划线
     * @param str
     * @param mx
     * @param my
     * @param mh
     * @param mw
     */
    public void drawTtitleString(String str, float mx, float my, float mw, float mh){
        float x = PageA4.mm2point(mx);
        float y = PageA4.mm2point(my);
        float w = PageA4.mm2point(mw);
        float h = PageA4.mm2point(mh);//即fontsize

        Font oldFont = g.getFont();
        Font font = new Font(oldFont.getName(), Font.PLAIN, Math.round(h));
        g.setFont(font);

        FontMetrics fm = g.getFontMetrics();
        y = y + h - fm.getDescent();

        Rectangle2D rc = fm.getStringBounds(str, g);
        x = x+(w-(int)rc.getWidth())/2;

        g.drawString(str, x, y);
    }

    public void fillRect(float mx, float my, float mw, float mh){
        g.fillRect((int)PageA4.mm2point(mx), (int)PageA4.mm2point(my), (int)PageA4.mm2point(mw), (int)PageA4.mm2point(mh));
    }


    private float mm2point(float mm){
        return PageA4.mm2point(mm);
    }

}
